package com.baymax.test.scene

import android.content.Context
import android.net.ConnectivityManager
import android.util.Log
import androidx.core.content.getSystemService
import androidx.multidex.MultiDexApplication
import com.baymax.lib.logger.KLog
import com.baymax.test.scene.plugin.PluginApplicationDelegate
import com.billy.cc.core.component.CC
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import org.koin.core.logger.Level
import org.koin.dsl.module

//import androidx.multidex.MultiDex

/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene
 * @class:       App
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2022/2/22 11:39
 * @Copyright (C) 2022 YSTEN
 * @author:       xuyujian
 */
class App : MultiDexApplication() {

    val plugin = PluginApplicationDelegate()


    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        println("getDefaultUncaughtExceptionHandler ${Thread.getDefaultUncaughtExceptionHandler()}")
        plugin.attachBaseContext(base, this)
        val app = this
        KLog.enableDebugLogConfig()
        startKoin {
            androidContext(app)
            androidLogger(level = Level.DEBUG)
            modules(module {
                single { app.getSystemService<ConnectivityManager>() }
            })
        }
//      kotlin.runCatching {
//          val ins = TestInstrument(app = this,)
//          val cls =
//              Class.forName("android.app.ActivityThread") // ActivityThread被隐藏了，所以通过这种方式获得class对象
//
//
//          val mthd: Method = cls.getDeclaredMethod(
//              "currentActivityThread") // 获取当前ActivityThread对象引用
//
//
//          var currentAT = mthd.invoke(null ,* emptyArray())
//          println("currentAT $currentAT")
//
//          val mInstrumentation: Field = currentAT.javaClass.getDeclaredField("mInstrumentation")
//
//          mInstrumentation.isAccessible = true //设置private变量为可读取
//
//          val get = mInstrumentation.get(currentAT)
//          ins.setDelegate(get as Instrumentation?)
//
//          mInstrumentation.set(currentAT, ins) // 修改ActivityThread.mInstrumentation值
//      }

    }

    override fun onCreate() {
        super.onCreate()
        Log.i("SceneApp", "onCreate: init")
        System.setProperty("kotlinx.coroutines.debug", "on")
        plugin.onCreate(this)
        CC.enableDebug(true)
        CC.enableVerboseLog(true)
        CC.enableRemoteCC(false)
    }
}